﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AlexandriaPressUserBusinessLayer;

namespace AlexandriaPressWebsite.MasterPages.UserPages
{
    public partial class CreateNewUser : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void createAccountButton_Click(object sender, EventArgs e)
        {
            UserBusinsessLayer userBusiness = new UserBusinsessLayer();

            int usernameCount = userBusiness.uniqueUsername(UserName.Text);
            int emailCount = userBusiness.uniqueEmail(Email.Text);

            if (usernameCount > 0)
                usernameExists.Visible = true;
            else
                usernameExists.Visible = false;

            if (emailCount > 0)
                emailExists.Visible = true;
            else
                emailExists.Visible = false;

            if (!AgreeCheckBox.Checked)
            {
                DisagreeLabel.Visible = true;
            }

            if (DateTime.Parse(Birthday.Text) > DateTime.Now)
            {
                dateStatLabel.Text = "Invalid birthdate.";
            }

            if (usernameCount == 0 && emailCount == 0 && AgreeCheckBox.Checked)
            {
                try
                {
                    userBusiness.createUser(FirstName.Text, LastName.Text, Gender.SelectedValue, Email.Text, Convert.ToDateTime(Birthday.Text), DateTime.Now, DeliveryAdd.Text, UserName.Text, Password.Text, Question.Text, Answer.Text);
                    ContentPlaceHolder content = (ContentPlaceHolder)this.Master.FindControl("formDoc");
                    content.Visible = false;
                    regStatLabel.Text = "Congratulations! Your account has been created. Kindly Log-in to view your Profile.";                                 
                }
                catch(Exception)
                {
                    regStatLabel.Text = "Sorry, an error occurred. Please try again.";
                }
            }
         
        }

    }
}
